package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(
        topic = "tanhua-log",consumerGroup = "tanhua-log-consumer"
)
public class LogMessageListener implements RocketMQListener<String> {

    @Autowired
    private LogService logService;

    @Override
    public void onMessage(String message) {
        Map map = JSON.parseObject(message, Map.class);
        String userId = (String) map.get("userId");
        String type =  (String) map.get("type");
        String date =  (String) map.get("date");

        //构造log对象
        Log log = new Log();
        log.setUserId(Long.valueOf(userId));
        log.setType(type);
        log.setLogTime(date);
        //调用service保存日志
        logService.save(log);
    }
}
